<?php
//模型
class Msg_templateModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'msg_template';
	}
	
	function get_list($arr,$page=1,$size=10){
		$where = ' 1 ';
		
		if(isset($arr['title']) && !empty($arr['title'])){
			$where .= " AND (title like '%{$arr['title']}%' OR content like '%{$arr['title']}%')";
		}
		if(isset($arr['date']) && !empty($arr['date'])){
			$where .= " AND DATE_FORMAT(addtime,'%Y-%m-%d')='{$arr['date']}' ";
		}
		
		if(isset($arr['date'])){
			$where .= " AND `type`='{$arr['type']}' ";
		}
		
		
		
		
		$res = $this->_where_get_list($where,$page,$size);
		return $res;
	}
	
	function get_info($id){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where id='{$id}' limit 1";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	
	//消息处理
	function notice_handle($user_id,$msg_name,$carr=array()){
		
		$info = $this->_field_get_info($msg_name,'msg_name');
		
		if(!$info){
			return false;
		}
		if($info['sys_is_send']==0){
			return false;
		}
		
		if(empty($info['groups'])){
			return false;
		}
		
		$this->load->model('Index/PrivgroupModel','PrivgroupModel');
		$res = $this->PrivgroupModel->ids_get_data($info['groups']);
		
		$uids = array();
		foreach($res as $val){
			if(!empty($val['ids'])){
				$ls_fg = explode(',',$val['ids']);
				$uids = array_merge($uids,$ls_fg);
			}
		}
		
		
		$title = $info['sys_title'];
		$content = $info['sys_content'];
		
		$time = time();
		$this->load->model('System/MessageModel','MessageModel');
		foreach($uids as $val){
			if(empty($val)){
				continue;
			}
			$arr = array(
				'user_id'=>$user_id,
				'title'=>$title,
				'content'=>$content,
				'addtime'=>$time,
				'target_id'=>$val,
				'msg_type'=>$info['id'],
				'type'=>0
			);
			$this->MessageModel->add($arr);
		}
		
		return true;
	}
	
}

